﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using ExportToExcelTools;
using Project5188.BUS;
using Project5188.BUS.DocumentManagement;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.DocumentManagement
{
    /// <summary>
    /// Interaction logic for DocumentPage.xaml
    /// </summary>
    public partial class DocumentPage
    {
        private CollectionViewSource _resources;

        public DocumentPage()
            : base(Extensions.PageNames.DocumentPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();
        }

        private void BindData()
        {
            var parameterValues = new ParameterValues();

            drpDocumentStatus.SelectedValuePath = "ID";
            drpDocumentStatus.IsSynchronizedWithCurrentItem = true;
            drpDocumentStatus.ItemTemplate = Resources["documentStatusTemplate"] as DataTemplate;
            drpDocumentStatus.ItemsSource = parameterValues.GetEntityByParameterId(27);
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtDocumentNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgDocuments.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<document> documentDetail = new Document();
            insert.SetEntity(documentDetail, Extensions.PageNames.DocumentPage.ToString());
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var documents = new Document();
                _resources = Resources["documentsViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    documents.GetEntityListByParameters(txtDocumentNo.Text, txtDocumentName.Text, txtVersion.Text,
                                                        Convert.ToInt64(drpDocumentStatus.SelectedValue)), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgDocuments.ItemsSource = _resources.Source as IEnumerable;
            }
            else if(TransactionInfo.TransactionId.HasValue)
            {
                var documents = new Document();
                _resources = Resources["documentsViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    documents.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgDocuments.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var documentValue = (document)dgDocuments.SelectedItem;
            var detail = new DetailWindow();
            DetailView<document> documentValueDetail = new Document { Entity = documentValue };
            detail.SetEntity(documentValueDetail, Extensions.PageNames.DocumentPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var documentValue = (document)dgDocuments.SelectedItem;
            
            if (documentValue.i_DocFlowTypeParameterValueID_28 != 375 || documentValue.g_DocumentStatusParameterValueID_27 == 374)
            {
                var update = new UpdateWindow();
                update.Closed += UpdateClosed;
                DetailView<document> documentValueDetail = new Document();
                update.SetEntity(documentValueDetail, Extensions.PageNames.DocumentPage.ToString(), documentValue.a_ID);
                update.Show();
            }
            else
            {
                MessageBox.Show("Bu döküman onay aşamasında güncellenemez");
            }
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var documentValue = (document)dgDocuments.SelectedItem;
            DetailView<document> documentValueDetail = new Document();
            GenerateDeleteDialog(documentValueDetail, documentValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgDocuments.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
